package com.yb0os1.serve.impl;

import com.yb0os1.context.BaseContext;
import com.yb0os1.dto.ShoppingCartDTO;
import com.yb0os1.entity.ShoppingCart;
import com.yb0os1.mapper.DishMapper;
import com.yb0os1.mapper.SetmealMapper;
import com.yb0os1.mapper.ShoppingCartMapper;
import com.yb0os1.serve.ShoppingCartServe;
import com.yb0os1.vo.DishVO;
import com.yb0os1.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServerImpl implements ShoppingCartServe {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addToShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //先判断当前商品是否在购物车中存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart goods = shoppingCartMapper.selectUserAndDishID(shoppingCart);
        //如果存在 那么number+1
        if (goods!=null){
            goods.setNumber(goods.getNumber()+1);
            shoppingCartMapper.update(goods);
        }else{
            //如果不存在 然后插入
            //要插入name和image 判断是套餐还是菜品
            // 套餐id
            Long setmealId = shoppingCart.getSetmealId();
            if (setmealId!=null){
                //套餐
                SetmealVO setmealVO = setmealMapper.selectSetmeal(setmealId);
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }else{
                //菜品
                DishVO dishVO = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setAmount(dishVO.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }
    @Override
    public List<ShoppingCart> getShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        Integer number = shoppingCartMapper.selectUserAndDishID(shoppingCart).getNumber();
        if(number>1){
            //多个相同的菜品 点一次-1
            shoppingCart.setNumber(number-1);
            shoppingCartMapper.update(shoppingCart);
        }else{
            shoppingCartMapper.deleteOne(shoppingCart);
        }
    }

    @Override
    public void clean() {
        shoppingCartMapper.cleanAll(BaseContext.getCurrentId());
    }
}
